package com.nmgsr.nettools.base;




import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nmgsr.nettools.R;
import com.nmgsr.nettools.util.LogUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

//import butterknife.ButterKnife;

public abstract class BaseActivity extends RxAppCompatActivity {

    protected View rootView;
    protected View view_status_bar;
    protected TextView tv_right, tv_center, tv_left;
    protected ImageView iv_status_bar, iv_left, iv_right;
    protected RelativeLayout rl_left, rl_right, fl_actionbar;
    protected FrameLayout fl_content;

    //    protected LoadService mBaseLoadService;
    protected ProgressDialog loadingDialog = null;

    public Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findView();
        initView();
        initData();
    }

    private void findView() {
        LogUtil.setTag(getClass().getSimpleName());
        LogUtil.e("----->onCreate");
        mContext = this;

        rootView = View.inflate(this, R.layout.root_view, null);
        setContentView(rootView);
        iv_status_bar = rootView.findViewById(R.id.iv_status_bar);
        view_status_bar = rootView.findViewById(R.id.view_status_bar);
        tv_left = rootView.findViewById(R.id.tv_left);
        tv_center = rootView.findViewById(R.id.tv_center);
        tv_right = rootView.findViewById(R.id.tv_right);
        iv_left = rootView.findViewById(R.id.iv_left);
        iv_right = rootView.findViewById(R.id.iv_right);
        rl_left = rootView.findViewById(R.id.rl_left);
        rl_right = rootView.findViewById(R.id.rl_right);
        fl_actionbar = rootView.findViewById(R.id.fl_actionbar);
        fl_content = rootView.findViewById(R.id.fl_content);

        tv_center.setText(getActionBarTitle() == null ? "" : getActionBarTitle());
        rl_left.setOnClickListener(v -> finish());

        View content = View.inflate(this, getLayoutId(), null);
        if (content != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            fl_content.addView(content, params);
//            mBaseLoadService = LoadSir.getDefault().register(content, (Callback.OnReloadListener) this::onNetReload);
        }
//        ButterKnife.bind(this);
    }

    protected abstract int getLayoutId();

    protected void initView() {
    }

    protected String getActionBarTitle() {
        return null;
    }

    protected void initData() {
    }

    protected void onNetReload(View v) {
        initData();
    }


    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.setTag(getClass().getSimpleName());
    }

    public void showLoading(boolean cancelable, String message) {
        if (loadingDialog == null) {
            loadingDialog = new ProgressDialog(this);
            loadingDialog.setCancelable(cancelable);

        }
        loadingDialog.show();
        loadingDialog.setMessage(message);
    }

    public void showLoading() {
        showLoading("");
    }

    public void showLoading(String message) {
        if (loadingDialog == null) {
            loadingDialog = new ProgressDialog(this);
        }
        loadingDialog.show();
        if (TextUtils.isEmpty(message)) {
            loadingDialog.setMessage("正在加载");
        } else {
            loadingDialog.setMessage(message);
        }
    }

    public void showLoadingCanNotCancel(String message) {
        if (loadingDialog == null) {
            loadingDialog = new ProgressDialog(this);
        }

        loadingDialog.setCancelable(false);
        loadingDialog.show();
        if (TextUtils.isEmpty(message)) {
            loadingDialog.setMessage("正在加载");
        } else {
            loadingDialog.setMessage(message);
        }
    }

    public void setLoadingText(String string) {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.setMessage(string);
        }
    }

    public void dismissLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissLoading();
    }

}
